from django.db import models
from django.contrib.auth.models import AbstractUser

from apps.utils.db import table_prefix, BaseModel, CoreModel
from conf.config import IMAGE_DOMAIN

# 密码最小长度
USERNAME_MIN_LEN = 5
# 密码最大长度
USERNAME_MAX_LEN = 20

# 密码最小长度
PASSWORD_MIN_LEN = 6
# 密码最大长度
PASSWORD_MAX_LEN = 20


class User(AbstractUser, BaseModel):
    """
    用户表
    """
    mobile = models.CharField(verbose_name='手机号', help_text="手机号", default='', max_length=11)
    avatar = models.CharField(max_length=255, verbose_name="头像", default='', null=True, blank=True, help_text="头像")

    def get_avatar(self, obj):
        if obj.avatar:
            return IMAGE_DOMAIN + obj.avatar
        return ""

    def get_roles(self, obj=None):
        if not isinstance(obj, type(None)) and obj:
            role_list = obj.role_set.filter(is_active=True).all()
            return role_list

    class Meta:
        db_table = table_prefix + 'users'
        verbose_name = '用户表'
        verbose_name_plural = verbose_name
